﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Wys.DataForm.CSharp.SoftWare;
using System.IO;

namespace Wys.DataForm.CSharp
{
    public partial class SoftWareAdd : Wys.Control.Vb.AutoAdjustFrom
    {
        #region 构造函数
        public SoftWareAdd()
        {
            InitializeComponent();
        }
        #endregion       

        #region 窗体加载时的初始数据
        public void LoadInitData()
        {
            cboSoftWareType1.Text = "";
            cboSoftWareType1.Tag = "-1";

            cboSoftWareType2.Text = "";
            cboSoftWareType2.Tag = "-1";

            cboSoftWareType3.Text = "";
            cboSoftWareType3.Tag = "-1";

            cboSoftWareType4.Text = "";
            cboSoftWareType4.Tag = "-1";

            cboSoftWareType5.Text = "";
            cboSoftWareType5.Tag = "-1";
        }
        #endregion

        #region 窗体初始化事件
        private void SoftWareAdd_Load(object sender, EventArgs e)
        {
            LoadInitData();           
            //LoadDBToForm();
        }
        #endregion      

        #region 选择附件按钮
        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                this.txtPath.Text = this.openFileDialog1.FileName;
                if(radFolder.Checked)
                {
                    RefreshEBook(this.txtPath.Text, "1");
                }
            }
        }
        #endregion       

        #region 确认按钮事件
        private void btnOk_Click(object sender, EventArgs e)
        {
            //先做CHECK
            if (String.IsNullOrEmpty(this.txtTitle.Text))
            {
                ShowMessageBox("标题不能为空");
                this.txtTitle.Focus();
                return;
            }

            //
            //if (String.IsNullOrEmpty(this.cboSoftWareType1.Text) && (String.IsNullOrEmpty(this.cboSoftWareType1.Text)))
            //{
            //    ShowMessageBox("类别不能为空");
            //    this.cboSoftWareType1.Focus();
            //    return;
            //}

            if (String.IsNullOrEmpty(this.cboSoftWareType1.Text) && String.IsNullOrEmpty(this.cboSoftWareType2.Text) && String.IsNullOrEmpty(this.cboSoftWareType3.Text) && String.IsNullOrEmpty(this.cboSoftWareType4.Text) && String.IsNullOrEmpty(this.cboSoftWareType5.Text))
            {
                ShowMessageBox("类别不能为空");
                this.cboSoftWareType1.Focus();
                return;
            }

            if (String.IsNullOrEmpty(this.txtPath.Text))
            {
                ShowMessageBox("路径不能为空");
                this.btnSelect.Focus();
                return;
            }

            if (!System.IO.File.Exists(this.txtPath.Text))
            {
                ShowMessageBox("路径不存在");
                this.btnSelect.Focus();
                return;
            }

            ////GUID
            System.Guid gd = new Guid();
            gd = Guid.NewGuid();
            String strFileNamePrefix = gd.ToString("N");
            System.IO.FileInfo fi = new System.IO.FileInfo(this.txtPath.Text);
            //String strExtension = fi.Extension;
            //2009-08-10
            String strExtension = fi.Name;
            //生成文件名
            //String strFileName = strFileNamePrefix + strExtension;
            //2009-08-10
            String strFileName = strExtension;
            String strFileName1 = System.IO.Path.Combine(strFileNamePrefix, strExtension);
            ADODB.Command cmd = new ADODB.Command();
            //D-2009-08-29
            //String strSql = "insert into XscSoftWare(Title,FileName,SoftWare,Memo) values('" + this.txtTitle.Text.Trim() + "','" + strFileName1 + "','" + this.cboSoftWareType.Text + "','" + this.txtMemo.Text+"')";
            //D-2009-08-30
            //String strSql = "insert into XscSoftWare(Title,FileName,SoftWare,Memo) values(?,?,?,?)";
            //D-2009-08-30
            //String strSql = "insert into XscSoftWare(Title,FileName,SoftWare,Memo,IsSingleFile) values(?,?,?,?,?)";//D-2009-10-10
            //A-2009-08-29
            String strSql = "insert into XscSoftWare(Title,FileName,type1,type2,type3,type4,type5,Memo,IsSingleFile,USERNAME,OPTIME,AddTime,LastOpenTime,OptUser) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";//A-2009-10-10
            ADODB.Parameter pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, this.txtTitle.Text.Trim().Length, this.txtTitle.Text.Trim());
            cmd.Parameters.Append(pa);

            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, strFileName1.Length, strFileName1);
            cmd.Parameters.Append(pa);

            //类型
            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput, 5, null);
            pa.Precision = 5;
            pa.NumericScale = 0;
            pa.Value=Convert.ToInt32(cboSoftWareType1.Tag);
            cmd.Parameters.Append(pa);

            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput, 5, null);
            pa.Precision = 5;
            pa.NumericScale = 0;
             pa.Value=Convert.ToInt32(cboSoftWareType2.Tag);
            cmd.Parameters.Append(pa);

            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput, 5, null);
            pa.Precision = 5;
            pa.NumericScale = 0;
             pa.Value=Convert.ToInt32(cboSoftWareType3.Tag);
            cmd.Parameters.Append(pa);

            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput, 5, null);
            pa.Precision = 5;
            pa.NumericScale = 0;
             pa.Value=Convert.ToInt32(cboSoftWareType4.Tag);
            cmd.Parameters.Append(pa);

            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput, 5, null);
            pa.Precision = 5;
            pa.NumericScale = 0;
             pa.Value=Convert.ToInt32(cboSoftWareType5.Tag);
            cmd.Parameters.Append(pa);

            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adWChar, ADODB.ParameterDirectionEnum.adParamInput, 1, null);
            if (this.txtMemo.Text.Trim().Length == 0)
            {
                pa.Size = 1;
            }
            else
            {
                pa.Size = this.txtMemo.Text.Trim().Length;
            }
            pa.Value = this.txtMemo.Text.Trim();
            cmd.Parameters.Append(pa);
            //A-2009-08-29
            //A-2009-08-30            
            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adInteger, ADODB.ParameterDirectionEnum.adParamInput, 1, null);
            pa.Precision = 2;
            pa.NumericScale = 0;
            if (this.radFile.Checked)
            {
                pa.Value = 0;
            }
            if (radFolder.Checked)
            {
                pa.Value = 1;
            }
            cmd.Parameters.Append(pa);

            //A-2009-10-10
            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName);
            cmd.Parameters.Append(pa);
            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, -1, DateTime.Now.ToString());
            cmd.Parameters.Append(pa);
            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, -1, DateTime.Now.ToString());
            cmd.Parameters.Append(pa);
            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adDBTimeStamp, ADODB.ParameterDirectionEnum.adParamInput, -1, DateTime.Now.ToString());
            cmd.Parameters.Append(pa);
            pa = cmd.CreateParameter("", ADODB.DataTypeEnum.adVarWChar, ADODB.ParameterDirectionEnum.adParamInput, strName.Length, strName);
            cmd.Parameters.Append(pa);
            //A-2009-10-10
            //A-2009-08-30
            cmd.CommandText = strSql;
            if (ct.ExcCommand(cmd))
            {
                try
                {
                    //添加程序按钮
                    //获取基本路径
                    String strBasePath = System.Configuration.ConfigurationManager.AppSettings["SoftWarePath"];
                    //String strCurrentPath = Environment.CurrentDirectory;
                    //2009-08-10
                    String strCurrentPath = GetCurrentDirectory();
                    String strPath = System.IO.Path.Combine(strCurrentPath, strBasePath);
                    //不存在根路径，就创建
                    if (!System.IO.Directory.Exists(strPath))
                    {
                        System.IO.Directory.CreateDirectory(strPath);
                    }

                    //查找基本目录
                    //System.Data.DataRowView dr = this.cboSoftWareType.SelectedValue as System.Data.DataRowView;
                    //strCurrentPath = dr.Row[1].ToString();

                    //////strCurrentPath = GetFolder(this.cboSoftWareType1.Tag.ToString());
                    //////strPath = System.IO.Path.Combine(strPath, strCurrentPath);

                    //不存在就创建
                    if (!System.IO.Directory.Exists(strPath))
                    {
                        System.IO.Directory.CreateDirectory(strPath);
                    }
                    //不存在就创建
                    strPath = System.IO.Path.Combine(strPath, strFileNamePrefix);
                    if (!System.IO.Directory.Exists(strPath))
                    {
                        System.IO.Directory.CreateDirectory(strPath);
                    }

                    String strPath3 = strPath;
                    String strPath4 = string.Empty;
                    strPath = System.IO.Path.Combine(strPath, strFileName);
                    //拷贝到目标路径
                    //D-2009-08-30
                    //fi.CopyTo(strPath);
                    //A-2009-08-30
                    if (radFile.Checked)
                    {
                        fi.CopyTo(strPath);
                    }
                    if (radFolder.Checked)
                    {
                        CopyFoler(fi.Directory.FullName, strPath3);
                    }
                    //A-2009-08-30

                }
                catch (Exception MyE)
                {
                    //SaveException(MyE);//D-2009-10-11
                    UTIL.CException.SaveExcepiton(UTIL.ErrorType.Error, MyE, MethodBase.GetCurrentMethod().GetType());//A-2009-10-11 
                }
                ShowMessageBox("添加成功");
                Form_Clear();
                this.bAdd = true;
                return;
            }
            ShowMessageBox("添加失败");
        }
        #endregion

        #region 取消按钮事件
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 窗体清空
        private void Form_Clear()
        {
            this.txtTitle.Text = "";
            this.txtPath.Text = "";
            //A-2009-08-30
            this.txtMemo.Text = "";
            this.radFile.Checked = true;
            this.radFolder.Checked = false;
            //A-2009-08-30
            cboSoftWareType1.Text = "";
            cboSoftWareType1.Tag = "-1";

            cboSoftWareType2.Text = "";
            cboSoftWareType2.Tag = "-1";

            cboSoftWareType3.Text = "";
            cboSoftWareType3.Tag = "-1";

            cboSoftWareType4.Text = "";
            cboSoftWareType4.Tag = "-1";

            cboSoftWareType5.Text = "";
            cboSoftWareType5.Tag = "-1";
            txtTitle.Focus();

        }
        #endregion        

        #region 重置按钮 A-2009-09-05
        private void btnReset_Click(object sender, EventArgs e)
        {
            Form_Clear();
        }
        private void btnUpdateForm_Click(object sender, EventArgs e)
        {
            WebSiteUpdate frm = new WebSiteUpdate();
            frm.iType = this.iType;
            frm.strName = this.strName;
            frm.ShowDialog();
        } 
        #endregion

        #region 类型选择
        private void btnSelectType_Click(object sender, EventArgs e)
        {
            String strN = "";
            String strName="";
            Button btn = sender as Button;
            if (btn == null)
            {
                return;
            }
            strName = btn.Name;
            strN = strName.Substring(strName.Length - 1, 1);
           
            Wys.Control.Vb.TreeTableFromBase ttf = new Wys.Control.Vb.TreeTableFromBase(类型.Controls["cboSoftWareType" + strN].Tag.ToString());
            ttf.ID = "ID";
            ttf.ParentID = "ParentID";
            ttf.DisplayName = "Name";
            ttf.TableName = "XscBugType";
            ttf.Text = "请选择软件类型";
            ttf.TypeId = 15;
            ttf.ShowDialog();
            if (!ttf.Cancel)
            {
                if (Convert.ToInt32(strN) > 1)
                {
                    for (int index = 1; index < Convert.ToInt32(strN); index++)
                    {
                        if (Convert.ToInt32(类型.Controls["cboSoftWareType" + index].Tag) == Convert.ToInt32(ttf.Result))
                        {
                            ShowMessageBox("类型不能重复");
                            return;
                        }
                    }
                }
                类型.Controls["cboSoftWareType" + strN].Text = ttf.DisplayNameValue;
                类型.Controls["cboSoftWareType" + strN].Tag = ttf.Result;
            }
            类型.Controls["cboSoftWareType" + strN].Text = ct.GetParentName(类型.Controls["cboSoftWareType" + strN].Tag.ToString(), "XscBugType", "Name", "ParentID", "ID");
        }

        public String GetFolder(String strid)
        {
            ADODB.Recordset rs = ct.GetTable("select FolderPath from XscSoftWareType where ID='" + strid + "'");
            if (rs == null)
                return "";
            if (rs.EOF)
            {
                rs.Close();
                rs = null;
                return "";
            }
            String str = rs.Fields["FolderPath"].Value.ToString();
            rs.Close();
            System.Runtime.InteropServices.Marshal.ReleaseComObject(rs); //20100531
            rs = null;
            return str;
        }
        #endregion               

        #region 扩展按钮单击
        private void btn扩展_Click(object sender, EventArgs e)
        {
            if (btn扩展.Enabled)
            {
                using (SelectedForm frm = new SelectedForm())
                {
                    if (btn扩展.Tag != null)
                    {
                        frm.文件夹路径 = btn扩展.Tag.ToString();
                        frm.ShowDialog();
                    }
                }
            }
        }
        #endregion

        #region 判断是否存在多个
        private void radFile_CheckedChanged(object sender, EventArgs e)
        {
            if (radFolder.Checked)
            {
                RefreshEBook(this.txtPath.Text, "1");
            }
            else
            {
                RefreshEBook(this.txtPath.Text, "0");
            }
        }

        public void RefreshEBook(String strFile, String IsSingle)
        {
            btn扩展.Visible = false;
            if (IsSingle != "1")
            {
                return;
            }
            if (System.IO.File.Exists(strFile))
            {
                String strExt = System.IO.Path.GetExtension(strFile).ToUpper();
                if (strExt == ".PDF" || strExt == ".PDG" || strExt == ".TXT" || strExt == ".DOC" || strExt == ".XST" || strExt == ".PPT" || strExt == ".CHM")
                {
                    String strDir = System.IO.Path.GetDirectoryName(strFile);
                    System.IO.DirectoryInfo dif = new DirectoryInfo(strDir);
                    if (dif.GetFiles().Length > 1)
                    {
                        btn扩展.Visible = true;
                    }
                }
                btn扩展.Tag = strFile;
            }
        }
        #endregion       

        #region 重载窗体数据保存 2010-09-06
        public override void 保存窗体数据到数据库(ref DataTable dtData)
        {
            dtData.Rows.Add(new string[] { "软件类型", "Tag", cboSoftWareType1.Tag.ToString() });
            dtData.Rows.Add(new string[] { "软件类型", "Text", cboSoftWareType1.Text.ToString() });
        }
        public override void 存取数据库数据到窗体(DataTable dtDbData)
        {
            if (dtDbData.Rows.Count > 0)
            {
                cboSoftWareType1.Tag = this.查找数据("软件类型", "Tag", "", ref dtDbData);
                cboSoftWareType1.Text = this.查找数据("软件类型", "Text", "", ref dtDbData);
            }
        }
        #endregion       

        private void cboSoftWareType1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb != null)
            {
                tb.Text = "";
                tb.Tag = "-1";
            }
        }
    }
}
